<?php

namespace app\api\controller;

use app\admin\model\AdLog;
use app\admin\model\GameRecord;
use app\admin\model\UserRecord;
use app\common\controller\Api;
use app\admin\model\Goods;
use app\admin\model\User;
use app\admin\model\CashOut;
use app\admin\model\FinishTask;
use app\admin\model\Task;
use think\Db;

/**
 * 兑换
 */
class Exchange extends Api {
	
	// 无需登录的接口,*表示全部
	protected $noNeedLogin = ['*'];
	// 无需鉴权的接口,*表示全部
	protected $noNeedRight = ['*'];
	
	/**
	 * 查询产品列表
	 *
	 * @ApiMethod   (POST)
	 * @ApiRoute    (/api/Sign/getSignInfo/uid/{uid})
	 * @ApiParams   (name="uid", type="integer", required=true, description="用户ID")
	 * @ApiParams   (name="type", type="integer", required=true, description="类型")
	 */
	public function getGoodsList() {
		$uid = $this->auth->id;
		$appid = $this->auth->app_id;
		$type = $this->request->request('type');// 0:美元，1:金币，2:提现记录，3:游戏关卡
		$page = $this->request->request('page');
		$pagesize = $this->request->request('pagesize');
		if (!empty($uid) && !empty($appid) && (is_numeric($type) === true)) {
			// 查询用户的资产以及是否验证PayPal
			$where['id'] = $uid;
			$user = User::where($where)->select();
			if(count($user)>0) {
				$paypal = $user[0]['paypal'];
				$dollar = $user[0]['dollar'];
				$coin = $user[0]['coin'];
				$card = $user[0]['card'];
			}
			switch($type) {
				case 0:
					$gList = $this->getGoodsByType($type);
					break;
				case 1:
					$gList = $this->getGoodsByType($type);
					break;
				case 2:
					//$gList = $this->getCashOutRecord($uid,$appid,$page,$pagesize);
                    $gList = $this->getTaskDay();
					break;
				case 3:
					//$gList = $this->getTaskDay();
                    $gList = $this->getCashOutRecord($uid,$appid,$page,$pagesize);
					break;
			}
		} else {
			$this->error(__('Invalid parameters'));
		}
		$date = [
			'paypal'=>$paypal,
			'dollar'=>$dollar,
			'coin'=>$coin,
			'card'=>$card,
			'gList'=>$gList
		];
		$this->success('successful', $date);
	}
	
	private function getGoodsByType($type) {
		$where['paytype'] = $type;
		$where['status'] = 0;
		$gl = Goods::where($where)->order('weigh','desc')->select();
		return $gl;
	}
	
	private function getCashOutRecord($uid,$appid,$page,$pagesize) {
		$gl = Db::table('sdk_cash_out')
		        ->alias('sco')
		        ->field('sg.*, sco.*')
		        ->join('sdk_goods sg','sco.goods_sku = sg.sku')
		        ->where("sco.user_id = '$uid' AND sco.app_id = '$appid' AND sco.type != '2'")
				->order('sco.id desc')
				->paginate($listRows=$pagesize, $simple = false, $config = [$page])->toArray();
		foreach ($gl['data'] as $k => &$v) {
            $v['arrival_time'] = date('Y-m-d H:i:s',strtotime($v['addtime'])+3600*72);
        }
		return $gl;
	}

	private function getTaskDay() {
		$where['status'] = 0;
		$gl = Task::where($where)->select();
        $uid = $this->auth->id;
        //$sku = FinishTask::where('user_id', $uid)->whereTime('addtime', 'today')->select();
        $sku = FinishTask::where('user_id', $uid)->whereTime('addtime', 'today')->column('task_sku');
        foreach ($gl as &$v) {
            if (in_array($v['sku'], $sku)) {
                $v['task_receive'] = true;
            } else {
                $v['task_receive'] = false;
            }

            switch($v['task_type']) {
                case 0:
                    $v['user_num'] = GameRecord::where('user_id', $uid)->where('game_id', $v['game_id'])->whereTime('addtime', 'today')->count();
                    break;
                case 1:
                    $v['user_num'] = AdLog::where('user_id', $uid)->whereTime('addtime', 'today')->count();
                    break;
                case 2:
                    $v['user_num'] = UserRecord::where('user_id', $uid)->where('type', 0)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                    break;
                case 3:
                    $v['user_num'] = UserRecord::where('user_id', $uid)->where('type', 1)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                    break;
                case 4:
                    $v['user_num'] = UserRecord::where('user_id', $uid)->where('type', 2)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                    break;
            }


        }
        return $gl;
	}
}